package org.freedoit.server.dao;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.freedoit.common.util.FreeDoitDataUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class DAOFactory {
	private static ProjectDAO projectDAO;
	private static TaskDAO taskDAO;
	private static TaskTypeDAO taskTypeDAO;
	private static UserDAO userDAO;

	private static final String EMF_NAME = "freedoit";
	
	private static EntityManagerFactory pmfInstance;

	static EntityManagerFactory getEntityManagerFactory() {
		if (pmfInstance == null) {
			Map<String, String> configVCap = new HashMap<String, String>();
			loadVCap(configVCap);
			pmfInstance = Persistence.createEntityManagerFactory(EMF_NAME, configVCap);
		}
		
		return pmfInstance;
	}
	
	private static void loadVCap(Map<String, String> config) {
		String strVcap = System.getenv("VCAP_SERVICES");
		System.out.println(strVcap);
		if (strVcap != null && !"".equals(strVcap)) {
			JsonParser jsonParser = new JsonParser();
			JsonElement e = null;
			
			try {
				e = jsonParser.parse(strVcap);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
			if (e != null) {
				JsonArray ja = e.getAsJsonObject().getAsJsonArray("mysql-5.1");
				if (ja != null && ja.size() > 0) {
					JsonElement eMysqlCredentials = ja.get(0).getAsJsonObject().get("credentials");
					if (eMysqlCredentials != null) {
						JsonObject jbMysqlCredentials = eMysqlCredentials.getAsJsonObject();
						
						// User Name
						JsonElement jeUsername = jbMysqlCredentials.get("username");
						if (jeUsername != null) {
							System.out.println(jeUsername.getAsString());
							config.put("hibernate.connection.username", jeUsername.getAsString());
						}
						
						// Password
						JsonElement jePassword = jbMysqlCredentials.get("password");
						if (jePassword != null) {
							System.out.println(jePassword.getAsString());
							config.put("hibernate.connection.password", jePassword.getAsString());
						}
						
						// Host, port and name
						JsonElement jeHost = jbMysqlCredentials.get("host");
						JsonElement jePort = jbMysqlCredentials.get("port");
						JsonElement jeName = jbMysqlCredentials.get("name");
						if (jeHost != null 
								&& jePort != null
								&& jeName != null) {
							String strUrl = FreeDoitDataUtils.buildJDBCURL("mysql", jeHost.getAsString(), 
									jePort.getAsString(), jeName.getAsString());
							System.out.println("JDBC URL: " + strUrl);
							config.put("hibernate.connection.url", strUrl);
						}
					}
				}
			}
		}
		
	}

	public static ProjectDAO getProjectDAO() {
		if (projectDAO == null) {
			projectDAO = new ProjectDAO();
		}
		
		return projectDAO;
	}

	public static TaskDAO getTaskDAO() {
		if (taskDAO == null) {
			taskDAO = new TaskDAO();
		}
		
		return taskDAO;
	}

	public static TaskTypeDAO getTaskTypeDAO() {
		if (taskTypeDAO == null) {
			taskTypeDAO = new TaskTypeDAO();
		}
		
		return taskTypeDAO;
	}

	public static UserDAO getUserDAO() {
		if (userDAO == null) {
			userDAO = new UserDAO();
		}
		
		return userDAO;
	}
}
